package com.edward.action;


import java.util.List;

import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import com.edward.JsonDateProcessor;
import com.edward.PagerModel;
import com.edward.model.Combo;
import com.edward.model.Fund;
import com.edward.model.FundPay;
import com.edward.model.FundRecei;
import com.edward.service.FundManager;
import com.edward.service.FundPayManager;
import com.edward.service.FundReceiManager;
import com.edward.service.ShallPayManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class FundManagerAction extends ActionSupport {


	private static final long serialVersionUID = -4852643111361308365L;

	private FundManager fundManager;
	
	private FundReceiManager fundReceiManager;
	
	private ShallPayManager shallPayManager;
	
	private FundRecei fundRecei;
	
	private FundPayManager fundPayManager;
	
	private FundPay fundPay;
	
	private Fund fund;
	
	private String type;
	
	private String choose;
	
	private int limit;
	
	private int start;
	
	int id = 0;
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	

	public String getChoose() {
		return choose;
	}

	public void setChoose(String choose) {
		this.choose = choose;
	}




	private boolean flag = false;
	
	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public FundRecei getFundRecei() {
		return fundRecei;
	}

	public void setFundRecei(FundRecei fundRecei) {
		this.fundRecei = fundRecei;
	}

	public void setFundReceiManager(FundReceiManager fundReceiManager) {
		this.fundReceiManager = fundReceiManager;
	}

	public void setShallPayManager(ShallPayManager shallPayManager) {
		this.shallPayManager = shallPayManager;
	}

	public Fund getFund() {
		return fund;
	}

	public void setFund(Fund fund) {
		this.fund = fund;
	}

	public void setFundManager(FundManager fundManager) {
		this.fundManager = fundManager;
	}
	
	public FundPay getFundPay() {
		return fundPay;
	}

	public void setFundPay(FundPay fundPay) {
		this.fundPay = fundPay;
	}

	public void setFundPayManager(FundPayManager fundPayManager) {
		this.fundPayManager = fundPayManager;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}


	@Secured("ROLE_COREFINANCE_ADD")
	public String add() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		fund.setRsum(fund.getRway() + fund.getRclose());
		fund.setDsum(fund.getDway() + fund.getDclose());
		fundManager.addFund(fund);
		 response.getWriter().print("{success:true,msg:'添加成功'}");
		return null;
	}
	
	@Secured("ROLE_COREFINANCE_UPDATE")
	public String update() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		fund.setRsum(fund.getRway() + fund.getRclose());
		fund.setDsum(fund.getDway() + fund.getDclose());
		fundManager.updateFund(fund);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_COREFINANCE_UPDATE")
	public String delete() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		fundManager.deleteFund(fund.getId());
		response.getWriter().print("{success : true , msg : '删除成功!'}");
		return null;
	}
	
	@Secured("ROLE_COREFINANCE")
	public String getList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
		pager = fundManager.pagerList(start, limit);
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		 
	           
		return null;
	}
	
	@Secured("ROLE_COREFINANCE_ADD")
	public String addRecei() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		fundReceiManager.addFundRecei(fundRecei);
		fundManager.addCheck(fundRecei.getHandler(),"handler");
		 response.getWriter().print("{success:true,msg:'添加成功'}");
		return null;
	}
	
	@Secured("ROLE_COREFINANCE_UPDATE")
	public String updateRecei() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		fundReceiManager.updateFundRecei(fundRecei);
		fundManager.addCheck(fundRecei.getHandler(),"handler");
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_COREFINANCE_UPDATE")
	public String deleteRecei() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		fundReceiManager.deleteFundRecei(fundRecei.getId());
		response.getWriter().print("{success : true , msg : '删除成功!'}");
		return null;
	}
	
	@Secured("ROLE_COREFINANCE")
	public String getReceiList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
		if(flag){
			pager = fundReceiManager.pagerList(start, limit,fundRecei);
		}else{
			pager = fundReceiManager.pagerList(start, limit,fundRecei.getType());
		}
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		 
	           
		return null;
	}
	
	@Secured("ROLE_COREFINANCE_ADD")
	public String addPay() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		if(id!=0){
			shallPayManager.updateForbid(fundPay,id);
		}
		fundPayManager.addFundPay(fundPay);
		fundPayManager.addChange(fundPay,choose);
		fundManager.addCheck(fundPay.getHandler(),"handler");
		 response.getWriter().print("{success:true,msg:'添加成功'}");
		return null;
	}
	
	@Secured("ROLE_COREFINANCE_UPDATE")
	public String updatePay() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		fundManager.addCheck(fundPay.getHandler(),"handler");
		fundPayManager.updateFundPay(fundPay);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_COREFINANCE_UPDATE")
	public String deletePay() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		fundPayManager.deleteFundPay(fundPay.getId());
		response.getWriter().print("{success : true , msg : '删除成功!'}");
		return null;
	}
	
	@Secured("ROLE_COREFINANCE")
	public String getPayList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
		if(flag){
			pager = fundPayManager.pagerList(start, limit,fundPay);
		}else{
			pager = fundPayManager.pagerList(start, limit,fundPay.getType());
		}
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	
	public String getCombo() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    List <Combo> list = fundManager.getComboList(type);
	    JSONArray jsonArray = JSONArray.fromObject(list);
	    String json = jsonArray.toString();
	    json  = "{\"handler\":"  + json+ "}";
	    response.getWriter().print(json);
	    
		return null;
	}
	
	
	@Secured("ROLE_COREFINANCE")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	
	
	
	
}
